home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap07 / Checker2 / Checker2.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  5.3 KB  |  174 lines

  1. /*-------------------------------------------------
  2.    CHECKER2.C -- Mouse Hit-Test Demo Program No. 2
  3.                  (c) Charles Petzold, 1998
  4.   -------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. #define DIVISIONS 5
  9.  
  10. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  11.  
  12. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  13.                     PSTR szCmdLine, int iCmdShow)
  14. {
  15.      static TCHAR szAppName[] = TEXT ("Checker2") ;
  16.      HWND         hwnd ;
  17.      MSG          msg ;
  18.      WNDCLASS     wndclass ;
  19.  
  20.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  21.      wndclass.lpfnWndProc   = WndProc ;
  22.      wndclass.cbClsExtra    = 0 ;
  23.      wndclass.cbWndExtra    = 0 ;
  24.      wndclass.hInstance     = hInstance ;
  25.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  26.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  27.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  28.      wndclass.lpszMenuName  = NULL ;
  29.      wndclass.lpszClassName = szAppName ;
  30.      
  31.      if (!RegisterClass (&wndclass))
  32.      {
  33.           MessageBox (NULL, TEXT ("Program requires Windows NT!"), 
  34.                       szAppName, MB_ICONERROR) ;
  35.           return 0 ;
  36.      }
  37.      
  38.      hwnd = CreateWindow (szAppName, TEXT ("Checker2 Mouse Hit-Test Demo"),
  39.                           WS_OVERLAPPEDWINDOW,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           CW_USEDEFAULT, CW_USEDEFAULT,
  42.                           NULL, NULL, hInstance, NULL) ;
  43.      
  44.      ShowWindow (hwnd, iCmdShow) ;
  45.      UpdateWindow (hwnd) ;
  46.      
  47.      while (GetMessage (&msg, NULL, 0, 0))
  48.      {
  49.           TranslateMessage (&msg) ;
  50.           DispatchMessage (&msg) ;
  51.      }
  52.      return msg.wParam ;
  53. }
  54.  
  55. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  56. {
  57.      static BOOL fState[DIVISIONS][DIVISIONS] ;
  58.      static int  cxBlock, cyBlock ;
  59.      HDC         hdc ;
  60.      int         x, y ;
  61.      PAINTSTRUCT ps ;
  62.      POINT       point ;
  63.      RECT        rect ;
  64.      
  65.      switch (message)
  66.      {
  67.      case WM_SIZE :
  68.           cxBlock = LOWORD (lParam) / DIVISIONS ;
  69.           cyBlock = HIWORD (lParam) / DIVISIONS ;
  70.           return 0 ;
  71.           
  72.      case WM_SETFOCUS :
  73.           ShowCursor (TRUE) ;
  74.           return 0 ;
  75.           
  76.      case WM_KILLFOCUS :
  77.           ShowCursor (FALSE) ;
  78.           return 0 ;
  79.           
  80.      case WM_KEYDOWN :
  81.           GetCursorPos (&point) ;
  82.           ScreenToClient (hwnd, &point) ;
  83.           
  84.           x = max (0, min (DIVISIONS - 1, point.x / cxBlock)) ;
  85.           y = max (0, min (DIVISIONS - 1, point.y / cyBlock)) ;
  86.           
  87.           switch (wParam)
  88.           {
  89.           case VK_UP :
  90.                y-- ;
  91.                break ;
  92.                
  93.           case VK_DOWN :
  94.                y++ ;
  95.                break ;
  96.                
  97.           case VK_LEFT :
  98.                x-- ;
  99.                break ;
  100.                
  101.           case VK_RIGHT :
  102.                x++ ;
  103.                break ;
  104.                
  105.           case VK_HOME :
  106.                x = y = 0 ;
  107.                break ;
  108.                
  109.           case VK_END :
  110.                x = y = DIVISIONS - 1 ;
  111.                break ;
  112.                
  113.           case VK_RETURN :
  114.           case VK_SPACE :
  115.                SendMessage (hwnd, WM_LBUTTONDOWN, MK_LBUTTON,
  116.                             MAKELONG (x * cxBlock, y * cyBlock)) ;
  117.                break ;
  118.           }
  119.           x = (x + DIVISIONS) % DIVISIONS ;
  120.           y = (y + DIVISIONS) % DIVISIONS ;
  121.           
  122.           point.x = x * cxBlock + cxBlock / 2 ;
  123.           point.y = y * cyBlock + cyBlock / 2 ;
  124.           
  125.           ClientToScreen (hwnd, &point) ;
  126.           SetCursorPos (point.x, point.y) ;
  127.           return 0 ;
  128.           
  129.      case WM_LBUTTONDOWN :
  130.           x = LOWORD (lParam) / cxBlock ;
  131.           y = HIWORD (lParam) / cyBlock ;
  132.           
  133.           if (x < DIVISIONS && y < DIVISIONS)
  134.           {
  135.                fState[x][y] ^= 1 ;
  136.                
  137.                rect.left   = x * cxBlock ;
  138.                rect.top    = y * cyBlock ;
  139.                rect.right  = (x + 1) * cxBlock ;
  140.                rect.bottom = (y + 1) * cyBlock ;
  141.                
  142.                InvalidateRect (hwnd, &rect, FALSE) ;
  143.           }
  144.           else
  145.                MessageBeep (0) ;
  146.           return 0 ;
  147.           
  148.      case WM_PAINT :
  149.           hdc = BeginPaint (hwnd, &ps) ;
  150.           
  151.           for (x = 0 ; x < DIVISIONS ; x++)
  152.           for (y = 0 ; y < DIVISIONS ; y++)
  153.           {
  154.                Rectangle (hdc, x * cxBlock, y * cyBlock,
  155.                           (x + 1) * cxBlock, (y + 1) * cyBlock) ;
  156.                     
  157.                if (fState [x][y])
  158.                {
  159.                     MoveToEx (hdc,  x   *cxBlock,  y   *cyBlock, NULL) ;
  160.                     LineTo   (hdc, (x+1)*cxBlock, (y+1)*cyBlock) ;
  161.                     MoveToEx (hdc,  x   *cxBlock, (y+1)*cyBlock, NULL) ;
  162.                     LineTo   (hdc, (x+1)*cxBlock,  y   *cyBlock) ;
  163.                }
  164.           }
  165.           EndPaint (hwnd, &ps) ;
  166.           return 0 ;
  167.                
  168.      case WM_DESTROY :
  169.           PostQuitMessage (0) ;
  170.           return 0 ;
  171.      }
  172.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  173. }
  174.